/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Discribe：
* CreateTime：2020-04-09 15:47:53
* Version：1.0
* Modify Recoder：
*************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TinyToolKit;
using TinyToolKit.Localization;

public class TinyAutoTranslation : MonoBehaviour 
{
    public Language curLan = Language.Unspecified;
    public string key;

    private Text text;
    private TLocalizationManager _localizationManagerMgr;

    private void Awake()
    {
        text = gameObject.GetComponent<Text>();
        _localizationManagerMgr = TinyTool.Instance.GetManager<TLocalizationManager>();
    }

    private void Start()
    {
        if (curLan == Language.Unspecified || curLan != _localizationManagerMgr.Language)
            SetText();
    }

    private void OnEnable()
    {
        _localizationManagerMgr.OnLanguageChanged += SetText;
    }

    private void OnDisable()
    {
        _localizationManagerMgr.OnLanguageChanged -= SetText;
    }

    private void SetText()
    {
        if (string.IsNullOrEmpty(key))
            TLogTool.Error(gameObject.name + "的key值为空");
        curLan = _localizationManagerMgr.Language;
        string content = _localizationManagerMgr.GetTranslation(key);
        if (text != null)
            text.text = content;
    }
}
